package pl.edu.project.view;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.text.NumberFormat;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.KeyStroke;

import javazoom.jl.player.Player;

@SuppressWarnings("serial")
public class AlarmPopup extends JDialog {

    private class ActionOK implements ActionListener {
	public void actionPerformed(ActionEvent e) {
	    dispose();
	    // dopisac
	}
    }
    private JButton okButton;
    Player player;
    private JLabel what;
    /**
	 * 
	 */
    private JLabel when;

    private JLabel where;

    public AlarmPopup(JFrame owner, boolean coolAlarm) {
	super(owner, "Przypomnienie");
	setSize(300, 130);
	setLocationRelativeTo(null);
	setDefaultCloseOperation(DISPOSE_ON_CLOSE);

	setData(coolAlarm);
	initWindow();

	if (coolAlarm)
	    play();
	else
	    Toolkit.getDefaultToolkit().beep();

	setResizable(false);
	setVisible(true);
    }

    private void initWindow() {
	Container contentPane = getContentPane();
	contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
	contentPane.add(when);
	contentPane.add(what);
	contentPane.add(where);
	contentPane.add(Box.createRigidArea(new Dimension(5, 5)));
	contentPane.add(okButton);
    }

    private void play() {
	try {
	    FileInputStream stream = new FileInputStream("data\\alarm.mp3");
	    BufferedInputStream bufferedStream = new BufferedInputStream(stream);
	    player = new Player(bufferedStream);
	} catch (Exception e) {
	    System.out.println("Nie można odtworzyć dzwonka.");
	    System.out.println(e);
	}

	try {
	    player.play();
	} catch (Exception e) {
	    System.out.println(e);
	}
    }

    private void setData(boolean coolAlarm) {
	NumberFormat timeFormat = NumberFormat.getInstance();
	timeFormat.setMinimumIntegerDigits(2);

	when.setFont(new Font("Tahoma", Font.BOLD, 16));
	when.setAlignmentX(CENTER_ALIGNMENT);
	what.setFont(new Font("Tahoma", Font.BOLD, 16));
	what.setAlignmentX(CENTER_ALIGNMENT);
	where.setFont(new Font("Tahoma", Font.BOLD, 16));
	where.setAlignmentX(CENTER_ALIGNMENT);
	this.okButton = new JButton("OK");
	okButton.setAlignmentX(CENTER_ALIGNMENT);
	okButton.addActionListener(new ActionOK());
	okButton.registerKeyboardAction(new ActionOK(),
		KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),
		JComponent.WHEN_IN_FOCUSED_WINDOW);
	okButton.registerKeyboardAction(new ActionOK(),
		KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false),
		JComponent.WHEN_IN_FOCUSED_WINDOW);
	okButton.registerKeyboardAction(new ActionOK(),
		KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false),
		JComponent.WHEN_IN_FOCUSED_WINDOW);
    }
}
